﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace N2.BlogEngine
{
	static class Extensions
	{
		public static void AdjustPublishedDate(this DateTime? published, bool isPublished)
		{
			if (!published.HasValue && isPublished) {
				published = DateTime.Now;
				return;
			}

			if (published.HasValue) {
				published = isPublished
					? new DateTime(Math.Min(published.Value.Ticks, DateTime.Now.Ticks))
					: default(DateTime?);
			}

		}

		public static ItemType ResolveItem<ItemType>(this ContentItem defaultContainer, int id)
			where ItemType: ContentItem
		{
			var _receiver = Context.Persister.Get<ItemType>(id);

			//TODO implement changing parent
			if (null == _receiver) {
				var _parent = defaultContainer;
				_receiver = Context.Definitions.CreateInstance<ItemType>(_parent);
				//_receiver.AddTo(_parent);//wtf?
				//_receiver.ID = id;
				//Context.Persister.Save(_parent);
			}

			return _receiver;
		}
	}
}
